package com.sx.controller;

import com.alibaba.fastjson.JSONObject;
import com.sx.basic.pageable.PageRequest;
import com.sx.basic.ResponseEntity;
import com.sx.basic.pageable.Condition;
import com.sx.entity.system.ConfigEntity;
import com.sx.mapping.SystemMapping;
import com.sx.service.system.IConfigGroupService;
import com.sx.service.system.IConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/11 15:29
 */
@RestController
public class SystemController {

    @Resource private IConfigService configService;
    @Resource private IConfigGroupService configGroupService;

    @GetMapping(SystemMapping.CONFIG) @ApiOperation("获取配置组下的所有配置")
    public ResponseEntity<List<ConfigEntity>> getConfig(@RequestParam("groupKey")String groupKey){
        //mybits
        //List<ConfigEntity> list = configService.findList(new Condition().eq("group_key", groupKey));
        //jpa
        List<ConfigEntity> list = configService.findList(new Condition().eq("groupKey", groupKey));

        return new ResponseEntity<>().toSuccess(list);
    }

    @GetMapping(SystemMapping.CONFIG_GROUP)@ApiOperation("获取全部配置组")
    public ResponseEntity getConfigGroup(@RequestParam(value = "page",required = false,defaultValue = "1")int page,
                                         @RequestParam(value = "size",required = false,defaultValue = "50")int size){
        return this.configGroupService.findPage(new Condition().eq("hidden",false),
                PageRequest.of(page,size, Sort.Direction.ASC,"sort"));
    }

    @PutMapping(SystemMapping.CONFIG)@ApiOperation("更新一个配置组")
    public ResponseEntity putConfig(@RequestBody JSONObject data){
        List<ConfigEntity> list =data.getJSONArray("list").toJavaList(ConfigEntity.class);
        this.configService.update(list);
        return new ResponseEntity().toSuccess();
    }


}
